package org.example.managere.controller;

import org.example.managere.dto.FMEAFMESDto;
import org.example.managere.dto.OperationDto;
import org.example.managere.service.FMEAFMESService;
import org.example.managere.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 批量数据操作控制器
 */
@RestController
@RequestMapping("/api/batch")
public class BatchOperationController {

    private final FMEAFMESService fmeafmesService;
    private final OperationService operationService;

    @Autowired
    public BatchOperationController(FMEAFMESService fmeafmesService, 
                                  OperationService operationService) {
        this.fmeafmesService = fmeafmesService;
        this.operationService = operationService;
    }

    /**
     * 批量删除FMEAFMES数据
     */
    @DeleteMapping("/fmeafmes")
    public ResponseEntity<?> batchDeleteFMEAFMES(@RequestBody List<Integer> ids) {
        int successCount = 0;
        Map<Integer, String> failedIds = new HashMap<>();
        
        for (Integer id : ids) {
            try {
                fmeafmesService.deleteFMEAFMES(id);
                successCount++;
            } catch (Exception e) {
                failedIds.put(id, e.getMessage());
            }
        }
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", successCount);
        response.put("total", ids.size());
        response.put("failed", failedIds);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 批量删除操作记录
     */
    @DeleteMapping("/operations")
    public ResponseEntity<?> batchDeleteOperations(@RequestBody List<Integer> ids) {
        int successCount = 0;
        Map<Integer, String> failedIds = new HashMap<>();
        
        for (Integer id : ids) {
            try {
                operationService.deleteOperation(String.valueOf(id));
                successCount++;
            } catch (Exception e) {
                failedIds.put(id, e.getMessage());
            }
        }
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", successCount);
        response.put("total", ids.size());
        response.put("failed", failedIds);
        
        return ResponseEntity.ok(response);
    }
} 